function [X_sorted, y_sorted] = randSortAndGroup(X, y, categories)

% Randomly sort the vectors in X, then group them by category.
%
% Parameters:
%  X - The matrix of input vectors, one per row.
%  y - The category or class label of the associated input vector.
%  categories - A column vector listing the category values in use by this data set.
%
% Returns:
%   The inputs X and y but with the vectors grouped by category and in a random
%   order within their category.

% Get the total number of input vectors.
totalVecs = size(X, 1);

% Get a random order of the indeces.
randOrder = randperm(totalVecs)';

% Sort the vectors and categories with the random order.
randVecs = X(randOrder, :);
randCats = y(randOrder, :);

X_sorted = [];
y_sorted = [];

% Re-group the vectors according to category.
for i = 1 : size(categories, 1)
    
    % Get the next category value.
    cat = categories(i);
    
    % Select all of the vectors for this category.
    catVecs = randVecs((randCats == cat), :);
    catCats = randCats((randCats == cat), :);
   
    % Append the vectors for this category.
    X_sorted = [X_sorted; catVecs];
    y_sorted = [y_sorted; catCats];
end

end